//手写离散化 + 差分写法
#include <algorithm>
#include <iostream>
#include <vector>

#define x first
#define y second

using namespace std;

const int N = 2e4 + 10;

typedef pair<int, int> PII;

int n;

PII a[N];
vector<int> lsh;

int cnt[N * 2];

int find(int x)
{
	int n = lsh.size();
	
	int l = 0, r = n - 1;
	
	while (l < r)
	{
		int mid = l + r >> 1;
		if (lsh[mid] >= x) r = mid;
		else l = mid + 1;
	}
	
	return l;
}

int main()
{
	cin >> n;
	
	for (int i = 0; i < n; i ++ ) 
	{
		cin >> a[i].x >> a[i].y;
		lsh.push_back(a[i].x);
		lsh.push_back(a[i].y);
	}
	
//	sort(a, a + n);

	sort(lsh.begin(), lsh.end());
	lsh.erase(unique(lsh.begin(), lsh.end()), lsh.end());
	
	
	
	for (int i = 0; i < n; i ++ )
	{
		int l = find(a[i].x);
		int r = find(a[i].y);
		
		cnt[l] ++ ; cnt [r] -- ;
	}
	
	int res = 0;
	
	int c = 0;
	int last = lsh[0];
	
	for (int i = 0; i < lsh.size(); i ++ )
	{
		c += cnt[i];
		if (last == -0x3f3f3f3f) last = lsh[i];
		
		if (c == 0)
		{
			res += lsh[i] - last;
			last = -0x3f3f3f3f;
		}
	}
	
	cout << res << endl;
	
	return 0;
	
}

//红黑树写法（map）
#include <iostream>
#include <map>

#define x first
#define y second

using namespace std;

map<int, int> mp;

int n;

int main()
{
	cin >> n;
	
	for (int i = 0; i < n; i ++ )
	{
		int l, r;
		cin >> l >> r;
		
		mp[l] ++ , mp[r] -- ;
	}
	
	int last = -0x3f3f3f3f;
	int c = 0;
	int res = 0;
	
	for (auto a : mp)
	{
		c += a.y;
		if (last == -0x3f3f3f3f) last = a.x;
		
		if (c == 0)
		{
			res += a.x - last;
			last = -0x3f3f3f3f;
		}
	}
	
	cout << res << endl;
	
	return 0;
}

